import Vue from 'vue';
import { message } from 'ant-design-vue';
import { getAnalysisDataByKeys } from '@/api/goods'
import { exportFile } from '@/api/api'
import { uploadToken } from '@/api/api'
Vue.prototype.$message = message;

const globalData = {
  uploadParams: {
    actionUrl: 'https://up-z0.qiniup.com/', // 七牛上传
    headers: {
      authorization: 'authorization-text'
    },
    uploadHost: 'https://img.bzhz.jnbygroup.com/'
  },
  uploadHost: 'https://img.bzhz.jnbygroup.com/'

}
export default globalData

// 上传图片获取token
export function fetchUploadToken () {
  return uploadToken().then(res => {
    if (res.code === 0) {
      return res.data
    } else {
      message.error(res.msg)
    }
  }).catch(err => {
    console.log('fetchUploadToken:' + err)
  })
}

// 导出
export function fetchExportFileId (param, serviceId) {
  let data = {
    params: JSON.stringify(param),
    serviceId: serviceId
  }
  return exportFile(data).then(res => {
    if (res.success) {
      return res.result
    } else {
      message.warning(res.message)
    }
  })
}

export function queryExportSuccess (timer, result, seconds, callback) {
  let startTime = new Date().getTime();
  timer = setInterval(() => {
    if (new Date().getTime() - startTime > seconds) {
      clearInterval(timer)
      message.warning('导出超时!')
      callback('done')
    } else {
      let params = {
        keys: result
      }
      return getAnalysisDataByKeys(params).then(res => {
        // console.log(res.data.success)
        if (res.code == 0) {
          if (res.data.success) {
            message.success('导出成功！')
            // window.open(res.data.url)
            clearInterval(timer)
            callback('done', res.data.url)
          }
        } else {
          message.warning(res.msg)
          clearInterval(timer)
          callback('done')
        }
      })
    }
  }, 2000)
}

// 导入
export function queryUploadSuccess (timer, result, seconds, callback) {
  let startTime = new Date().getTime();
  let that = this
  timer = setInterval(() => {
    if (new Date().getTime() - startTime > seconds) {
      clearInterval(timer)
      message.warning('导入超时!')
      callback('warning')
    } else {
      let params = {
        keys: result
      }
      getAnalysisDataByKeys(params).then(res => {
        if (res.code == 0) {
          if (res.data.success) {
            if (res.data.url) {
              message.warning('部分数据上传失败')
              /* url返回格式(string)：url; sum; success; fail */
              if (!res.data.url.includes(';')) window.open(res.data.url)

            } else {
              message.success(res.msg)
            }
            clearInterval(timer)
            callback('done', res.data)
          }
        } else {
          message.warning(res.msg)
          clearInterval(timer)
          callback('warning')
        }
      })
    }
  }, 2000)
}
/** 下载文件自定义文件名 */
export function onDownFile (url, filename) {
  getBlob(url, (blob) => {
    saveAs(blob, filename)
  })
}

export function getBlob (url, cb) {
  var xhr = new XMLHttpRequest()
  xhr.open('GET', url, true)
  xhr.responseType = 'blob'
  xhr.onload = function () {
    if (xhr.status === 200) {
      cb(xhr.response)
    }
  }
  xhr.send()
}

export function saveAs (blob, filename) {
  if (window.navigator.msSaveOrOpenBlob) {
    navigator.msSaveBlob(blob, filename)
  }
  else {
    var link = document.createElement('a')
    var body = document.querySelector('body')

    link.href = window.URL.createObjectURL(blob)
    link.download = filename

    link.style.display = 'none'
    body.appendChild(link)

    link.click()
    body.removeChild(link)
    window.URL.revokeObjectURL(link.href)
  }
}